package de.blitzcoder.cddatabase.gui.tracktable;

import de.blitzcoder.cddatabase.data.CD;
import de.blitzcoder.cddatabase.data.Data;
import de.blitzcoder.cddatabase.data.Track;
import de.blitzcoder.cddatabase.gui.EditCDDialog;
import de.blitzcoder.cddatabase.gui.datatable.DataTable;
import java.awt.Frame;
import java.util.LinkedList;
import java.util.ListIterator;

/**
 *
 * @author blitzcoder
 */
public class TrackTable extends DataTable {

    private static final String[]   COLUMNS         = new String[] {"Name", "Künstler", "CD", "CD-ID", "Länge"};
    private static final int[]      COLUMNWIDTHS    = new int[] {0,0,0,40,50};

    public TrackTable() {
        super(COLUMNS,COLUMNWIDTHS);
        updateTrackList();
    }

    public void updateTrackList() {
        getModel().clear();
        LinkedList<TrackTableRow> trackList = new LinkedList<TrackTableRow>();
        ListIterator<CD> it = Data.cdList.listIterator();
        while (it.hasNext()) {
            CD cd = it.next();
            Track[] tracks = cd.getTracks();
            for (Track t:tracks)
                trackList.addLast(new TrackTableRow(t, cd));
        }
        getModel().addRows(trackList.toArray(new TrackTableRow[0]));
        System.out.println("Added "+trackList.size()+" Tracks");
    }

    public boolean editCurrentCD(Frame parent) {
        int r = getSelectedRow();
        if (r == -1)
            return false;

        TrackTableRow row = (TrackTableRow) getModel().getRow(r);

        EditCDDialog dialog = new EditCDDialog(parent, row.getCD());
        dialog.setVisible(true);

        return true;
    }

    
}
